package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.*;
import cn.web.tunnelrobot.dto.*;
import cn.web.tunnelrobot.pojo.*;
import cn.web.tunnelrobot.service.UnitInfoService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UnitInfoServiceImpl implements UnitInfoService {
    @Autowired
    private UnitInfoDao unitInfoDao;
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private WorkAreaDao workAreaDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRecordDao userRecordDao;

    @Override
    public HashMap<String, Object> addUnitInfo(UnitInfo1 unitInfo) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }
        if (unitInfo.getUiLevel() == 1){
            unitInfo.setUiParentId(null);
        }
        if (unitInfo.getOperator() == null){
            map.put("code",-1);
            map.put("msg","操作人不能为空");
            return map;
        }
        User user = new User();
        user.setUsId(unitInfo.getOperator());
        User user1 = userDao.getUserInfo(user);
        if (user1.getUsRole() == 2){
            map.put("code",-1);
            map.put("msg","暂无权限");
            return map;
        }
        WorkArea workArea1 = workAreaDao.getWorkAreaByUsId(unitInfo.getUsId());
        if (workArea1 != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责标段");
            return map;
        }
        UnitInfo unitInfo1 = unitInfoDao.getUnitInfoByUsId(unitInfo.getUsId());
        if (unitInfo1 != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责施工单位");
            return map;
        }
        Project project = projectDao.getProjectByUsId(unitInfo.getUsId());
        if (project != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责项目");
            return map;
        }
        unitInfo.setUiCreateDate(new Date());
        UnitInfo unitInfo2 = unitInfoDao.getUnitInfoByName(unitInfo.getUiParentId(),unitInfo.getUiName());
        if (unitInfo2 != null){
            map.put("code",-1);
            map.put("msg","该名称已添加,请勿重复添加");
            return map;
        }
        int result = unitInfoDao.addUnitInfo(unitInfo);
        if (result == 1){
            map.put("msg","添加成功");
            map.put("code",0);
            /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            if (unitInfo.getUiLevel() == 1){
                userRecord.setUrOperatContent("添加公司信息");
            }else {
                userRecord.setUrOperatContent("添加分局信息");
            }
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updateUnitInfo(UnitInfo1 unitInfo) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }
        User user = new User();
        user.setUsId(unitInfo.getOperator());
        User user1 = userDao.getUserInfo(user);
        if (user1.getUsRole() == 2){
            map.put("code",-1);
            map.put("msg","暂无权限");
            return map;
        }

        unitInfo.setUiUpdateDate(new Date());
        unitInfo.setUiLevel(null);
        unitInfo.setUiParentId(null);
        UnitInfo unitInfo1 = unitInfoDao.getUnitInfoById(unitInfo.getUiId());
        if (unitInfo.getUsId() != unitInfo1.getUsId()){
            WorkArea workArea1 = workAreaDao.getWorkAreaByUsId(unitInfo.getUsId());
            if (workArea1 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责标段");
                return map;
            }
            UnitInfo unitInfo3 = unitInfoDao.getUnitInfoByUsId(unitInfo.getUsId());
            if (unitInfo3 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责施工单位");
                return map;
            }
            Project project2 = projectDao.getProjectByUsId(unitInfo.getUsId());
            if (project2 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责项目");
                return map;
            }
        }
        if (!unitInfo1.getUiName().equals(unitInfo.getUiName())){
            UnitInfo unitInfo2 = unitInfoDao.getUnitInfoByName(unitInfo1.getUiParentId(),unitInfo.getUiName());
            if (unitInfo2 != null){
                map.put("code",-1);
                map.put("msg","该名称已存在");
                return map;
            }
        }
        int result = unitInfoDao.updateUnitInfo(unitInfo);
        if (result == 1){
            map.put("code",0);
            map.put("msg","修改成功");
            /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            if (unitInfo.getUiLevel() == 1){
                userRecord.setUrOperatContent("修改公司信息");
            }else {
                userRecord.setUrOperatContent("修改分局信息");
            }
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getUnitInfoList() {
        HashMap<String,Object> map = new HashMap<String,Object>();
        List<UnitInfoDTO1> unitInfoList = new ArrayList<>();
        try {
            unitInfoList = unitInfoDao.getUnitInfoListReturnUnitInfoDTO1(null,1);
            int i=1;
            if (unitInfoList.size() > 0){
                for (UnitInfoDTO1 unitInfo:unitInfoList) {
                    List<UnitInfoDTO1> unitInfoList1 = unitInfoDao.getUnitInfoListReturnUnitInfoDTO1(unitInfo.getId(),2);
                    List<UnitInfoDTO2> unitInfoDTO2List = EntityUtils.copyList(unitInfoList1, UnitInfoDTO2.class);
                    unitInfo.setChildren(unitInfoDTO2List);
                    unitInfo.setKey(i+"-0"+"-0"+"-0");
                    int j=1;
                    if (unitInfoList1.size() > 0){
                        for (UnitInfoDTO2 unitInfo1:unitInfoDTO2List) {
                            List<ProjectDTO1> projectList = projectDao.getProjectListReturnProjectDTO1(unitInfo1.getId());
                            unitInfo1.setChildren(projectList);
                            unitInfo1.setKey(i+"-"+j+"-0"+"-0");
                            int k = 1;
                            if (projectList.size() > 0){
                                for (ProjectDTO1 projectDTO1:projectList) {
                                    List<WorkAreaDTO1> workAreaList =  workAreaDao.getWorkAreaListReturnWorkAreaDTO1(projectDTO1.getId());
                                    projectDTO1.setChildren(workAreaList);
                                    projectDTO1.setKey(i+"-"+j+"-"+k+"-0");
                                    int l = 1;
                                    for (WorkAreaDTO1 workAreaDTO1:workAreaList) {
                                        workAreaDTO1.setKey(i+"-"+j+"-"+k+"-"+l);
                                        workAreaDTO1.setLevel("4");
                                        l++;
                                    }
                                    k++;
                                }
                            }
                            j++;
                        }
                    }
                    i++;
                }
                /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                UserRecord userRecord = new UserRecord();
                userRecord.setUsId(user2.getUsId());
                userRecord.setUrOperatContent("查询监理信息左侧导航栏");
                userRecord.setUrOperatTime(new Date());
                userRecordDao.addUserRecord(userRecord);*/
            }
            map.put("code",0);
            map.put("data",unitInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    @Override
    public HashMap<String, Object> deleteUnitInfoByUiId(Integer id, Integer uiLevel, Integer usId) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }
        User user = new User();
        user.setUsId(usId);
        User user1 = userDao.getUserInfo(user);
       /* if (user1.getUsRole() == 2){
            map.put("code",-1);
            map.put("msg","暂无权限");
            return map;
        }*/
        int result = 0;
        if (uiLevel == 1){
            List<UnitInfo>list = unitInfoDao.getUnitInfoList(id,null);
            if (list.size() > 0){
                map.put("code",-1);
                map.put("msg","该监理单位下存在分局，请先删除分局");
                return map;
            }
            result  = unitInfoDao.deleteUnitInfoByUiId(id);
        }else if (uiLevel == 2){
            List<Project> list = projectDao.getProjectList(id);
            if (list.size() > 0){
                map.put("code",-1);
                map.put("msg","该施工单位下存在项目信息，请先删除项目");
                return map;
            }
            result  = unitInfoDao.deleteUnitInfoByUiId(id);
        }else if(uiLevel == 3){
            if(id == 1){
                map.put("code",-1);
                map.put("msg","该项目不能删除");
                return map;
            }
            List<WorkArea> list = workAreaDao.getWorkAreaList(id);
            if (list.size() > 0){
                map.put("code",-1);
                map.put("msg","该项目下存在标段信息，请先删除标段信息");
                return map;
            }
            result = projectDao.deleteProjectByPrId(id);
        }else if(uiLevel == 4){
            result = workAreaDao.deleteWorkAreaByWaId(id);
        }
        if (result == 1){
            map.put("code",0);
            map.put("msg","删除成功");
           /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("删除监理信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getUnitInfoListPage(String companyName, String branchName, String projectName, String workAreaName, String usName, Integer id, Integer position, Integer uiLevel, Integer pageNo, Integer pageSize) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        if (uiLevel == 1){
            List<UnitInfo> list = unitInfoDao.getUnitInfoListCompany(companyName, ((pageNo - 1) * pageSize), pageSize);
            if (list != null) {
                map.put("code", 0);
                map.put("msg", "");
                map.put("count",unitInfoDao.getUnitInfoCountCompany(companyName));
                map.put("data", list);
               /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                UserRecord userRecord = new UserRecord();
                userRecord.setUsId(user2.getUsId());
                userRecord.setUrOperatContent("分页查询公司列表");
                userRecord.setUrOperatTime(new Date());
                userRecordDao.addUserRecord(userRecord);*/
            } else {
                map.put("code", -1);
                map.put("msg", "查询失败");
            }
        }
        if (uiLevel == 2){
            if (position == 1){
                List<UnitInfoDTO> list = unitInfoDao.getUnitInfoListBranchById(id,((pageNo - 1) * pageSize), pageSize);
                if (list != null) {
                    map.put("code", 0);
                    map.put("msg", "");
                    map.put("count",unitInfoDao.getUnitInfoCountBranchById(id));
                    map.put("data", list);
                    /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                    UserRecord userRecord = new UserRecord();
                    userRecord.setUsId(user2.getUsId());
                    userRecord.setUrOperatContent("分页查询分局列表");
                    userRecord.setUrOperatTime(new Date());
                    userRecordDao.addUserRecord(userRecord);*/
                } else {
                    map.put("code", -1);
                    map.put("msg", "查询失败");
                }
            }else {
                List<UnitInfoDTO> list = unitInfoDao.getUnitInfoListBranch(id,companyName,branchName,usName, ((pageNo - 1) * pageSize), pageSize);
                if (list != null) {
                    map.put("code", 0);
                    map.put("msg", "");
                    map.put("count",unitInfoDao.getUnitInfoCountBranch(id,companyName,branchName,usName));
                    map.put("data", list);
                   /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                    UserRecord userRecord = new UserRecord();
                    userRecord.setUsId(user2.getUsId());
                    userRecord.setUrOperatContent("分页查询分局列表");
                    userRecord.setUrOperatTime(new Date());
                    userRecordDao.addUserRecord(userRecord);*/
                } else {
                    map.put("code", -1);
                    map.put("msg", "查询失败");
                }
            }

        }
        if (uiLevel == 3){
            if (position == 1){
                List<ProjectDTO> list = projectDao.getProjectListPageByUiId(id, ((pageNo - 1) * pageSize), pageSize);
                if (list != null) {
                    map.put("code", 0);
                    map.put("msg", "");
                    map.put("count",projectDao.getProjectCountPageByUiId(id));
                    map.put("data", list);
                   /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                    UserRecord userRecord = new UserRecord();
                    userRecord.setUsId(user2.getUsId());
                    userRecord.setUrOperatContent("分页查询项目列表");
                    userRecord.setUrOperatTime(new Date());
                    userRecordDao.addUserRecord(userRecord);*/
                } else {
                    map.put("code", -1);
                    map.put("msg", "查询失败");
                }
            }else {
                List<ProjectDTO> list = projectDao.getProjectListPage(id,companyName,branchName,projectName,usName, ((pageNo - 1) * pageSize), pageSize);
                if (list != null) {
                    map.put("code", 0);
                    map.put("msg", "");
                    map.put("count",projectDao.getProjectCountPage(id,companyName,branchName,projectName,usName));
                    map.put("data", list);
                   /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                    UserRecord userRecord = new UserRecord();
                    userRecord.setUsId(user2.getUsId());
                    userRecord.setUrOperatContent("分页查询项目列表");
                    userRecord.setUrOperatTime(new Date());
                    userRecordDao.addUserRecord(userRecord);*/
                } else {
                    map.put("code", -1);
                    map.put("msg", "查询失败");
                }
            }
        }
        if (uiLevel == 4){
            if (position == 1){
                List<WorkAreaDTO> list = workAreaDao.getWorkAreaListPageByPrId(id, ((pageNo - 1) * pageSize), pageSize);
                if (list != null) {
                    map.put("code", 0);
                    map.put("msg", "");
                    map.put("count",workAreaDao.getWorkAreaCountPageByPrId(id));
                    map.put("data", list);
                   /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                    UserRecord userRecord = new UserRecord();
                    userRecord.setUsId(user2.getUsId());
                    userRecord.setUrOperatContent("分页查询标段列表");
                    userRecord.setUrOperatTime(new Date());
                    userRecordDao.addUserRecord(userRecord);*/
                } else {
                    map.put("code", -1);
                    map.put("msg", "查询失败");
                }
            }else {
                List<WorkAreaDTO> list = workAreaDao.getWorkAreaListPage(id,projectName,workAreaName,usName,((pageNo - 1) * pageSize), pageSize);
                if (list != null) {
                    map.put("code", 0);
                    map.put("msg", "");
                    map.put("count",workAreaDao.getWorkAreaCountPage(id,projectName,workAreaName,usName));
                    map.put("data", list);
                    /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                    UserRecord userRecord = new UserRecord();
                    userRecord.setUsId(user2.getUsId());
                    userRecord.setUrOperatContent("分页查询标段列表");
                    userRecord.setUrOperatTime(new Date());
                    userRecordDao.addUserRecord(userRecord);*/
                } else {
                    map.put("code", -1);
                    map.put("msg", "查询失败");
                }
            }
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getUnitInfoListByParentId(Integer uiParentId, Integer uiLevel) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        List<UnitInfo> unitInfoList = null;
        if (uiLevel == 1){
            unitInfoList = unitInfoDao.getUnitInfoListByParentId(null,uiLevel);
        }
        if (uiLevel == 2){
            unitInfoList = unitInfoDao.getUnitInfoListByParentId(uiParentId,uiLevel);
        }
        map.put("code",0);
        map.put("data",unitInfoList);
        return map;
    }
    @Override
    public HashMap<String, Object> getUnitInfoById(Integer uiId, Integer uiLevel) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        if (uiLevel == 1){
            UnitInfo unitInfoById = unitInfoDao.getUnitInfoById(uiId);
            if (unitInfoById != null){
                map.put("code",0);
                map.put("data",unitInfoById);
            }else {
                map.put("code",-1);
                map.put("msg","查询失败");
            }
        }
        if (uiLevel == 2){
            //获取分局
            UnitInfo unitInfoById = unitInfoDao.getUnitInfoBranchById(uiId);
            if (unitInfoById != null){
                map.put("code",0);
                map.put("data",unitInfoById);
            }else {
                map.put("code",-1);
                map.put("msg","查询失败");
            }
        }
        return map;
    }
}
